package grex.Nodes;

import grex.Data.Prediction;
import grex.Data.PredictionContainer;

public class Equals extends Node implements ICategoricalOperator {

    private static final int NROFCILDREN = 2;

    public Equals() {
        super(NROFCILDREN);
    }

    public synchronized void execute(Node parent, PredictionContainer data, int mode) throws GeneException {
        this.parent = parent;
        Node[] childs = this.getChildren();
      /*  PredictionContainer lResult = data;
        PredictionContainer rResult = data.clone();
        childs[0].execute(this, lResult, mode);
        childs[1].execute(this, rResult, mode);
        for (double[] key : data.keySet()) {
            if (lResult.get(key).getPrediction() == rResult.get(key).getPrediction()) {
                data.get(key).setPrediction(1);
            } else {
                data.get(key).setPrediction(0);
            }
        }*/
      
          
      for(Prediction prediction:data.values()){
        if(((IOperatorTerminal)childs[0]).execute(this, prediction) == ((IOperatorTerminal)childs[1]).execute(this, prediction))
            prediction.setCondition(true);
           else 
            prediction.setCondition(false);
      }
    }

    public Object clone() {
        Equals newEquals = new Equals();
        newEquals.setOptions(ops);
        newEquals.setEnvironment(environment);
        cloneChildren(newEquals);
  //      newEquals.setParent(parent);
        return newEquals;
    }

      public boolean equals(Object o){
        if(o instanceof Equals){
            Equals g =(Equals) o;
            Node[] oChildren = g.getChildren();
            Node[] children = getChildren();
            Var  var,varO;
            Val val,valO;
            if(oChildren[0] instanceof Var ){
                varO=(Var) oChildren[0];
                valO=(Val) oChildren[1];
            }else{
                varO=(Var) oChildren[1];
                valO=(Val) oChildren[0];
            }
            if(children[0] instanceof Var ){
                var=(Var) children[0];
                val=(Val) children[1];
            }else{
                var=(Var) children[1];
                val=(Val) children[0];
            }

            if(var.equals(varO)){
                if(val.equals(valO))
                    return true;
                if(environment.getNumberOfSplits(var.getVarNr(), ops.getTestFOLD())==2){
                    return true;
                }

            }
           /* if(getChildren()[0].equals(g.getChildren()[0]))
                if(getChildren()[1].equals(g.getChildren()[1]))
                    return true;//*/
        }
        return false;
    }

    public String toString() {
        return "==";
    }
}
